﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_SheetBookComp : DockContent
    {
        private DGBY.BLL.DZCompDataTab m_bllDZCompDataTab = new DGBY.BLL.DZCompDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectDZCompDataTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        private Dictionary<string, string> dicQueryCondition = new Dictionary<string, string>();

        public Frm_SheetBookComp()
        {
            InitializeComponent();
        }
        private void Frm_SheetBookComp_Load(object sender, EventArgs e)
        {
            InitDipQueryCondition();
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelete.Visible = true;
            }
        }

        private void InitDipQueryCondition()
        {
            dicQueryCondition.Add("单证公司名称", "DZCompName");
            dicQueryCondition.Add("单证公司简称", "DZCompNameJX");
            dicQueryCondition.Add("公司地址", "DZCompAddr");
            dicQueryCondition.Add("公司电话", "DZCompTel");
            dicQueryCondition.Add("公司传真", "DZCompFax");
            dicQueryCondition.Add("经理姓名", "ManagerName");
            dicQueryCondition.Add("经理电话", "ManagerTel");
            dicQueryCondition.Add("联系人姓名", "DZConPeoName");
            dicQueryCondition.Add("联系人电话", "DZConPeoTel");
            dicQueryCondition.Add("开户行名称", "DZKHBank");
            dicQueryCondition.Add("开户行帐号", "DZCountNo");
            dicQueryCondition.Add("备注", "Note");
            this.comboBoxQueryCondition.SelectedIndex = 0;
            this.radioButtonValueIs.Select();
        }

        private void buttonCompConsume_Click(object sender, EventArgs e)
        {
            Frm_SheetBookConsume frm_SheetBookConsume = new Frm_SheetBookConsume();
            frm_SheetBookConsume.StartPosition = FormStartPosition.CenterScreen;
            frm_SheetBookConsume.ShowDialog();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewCompList.DataSource = null;
            m_ds = m_bllDZCompDataTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewCompList.DataSource = m_ds.Tables[0];
                dataGridViewCompList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewCompList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strDZCompDataTabID = this.dataGridViewCompList.Rows[this.dataGridViewCompList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectDZCompDataTabID = Convert.ToInt32(strDZCompDataTabID, 10);
            cellclickDZComp();
        }

        private void cellclickDZComp()
        {
            DGBY.Model.DZCompDataTab modelDZCompDataTab = m_bllDZCompDataTab.GetModel(m_nSelectDZCompDataTabID);
            if (modelDZCompDataTab != null)
            {
                this.textBoxCompName.Text = modelDZCompDataTab.DZCompName;
                this.textBoxCompAbbr.Text = modelDZCompDataTab.DZCompNameJX;
                this.textBoxCompPhone.Text = modelDZCompDataTab.DZCompTel;
                this.textBoxConPName.Text = modelDZCompDataTab.DZConPeoName;
                this.textBoxConPPhone.Text = modelDZCompDataTab.DZConPeoTel;
                this.textBoxAddress.Text = modelDZCompDataTab.DZCompAddr;
                this.textBoxBank.Text = modelDZCompDataTab.DZKHBank;
                this.textBoxAccount.Text = modelDZCompDataTab.DZCountNo;
                this.textBoxFax.Text = modelDZCompDataTab.DZCompFax;
                this.textBoxManagerName.Text = modelDZCompDataTab.ManagerName;
                this.textBoxMngTel.Text = modelDZCompDataTab.ManagerTel;
                this.richTextBoxNote.Text = modelDZCompDataTab.Note;
            }
            else
            {
                this.textBoxCompName.Text = "";
                this.textBoxCompAbbr.Text = "";
                this.textBoxCompPhone.Text = "";
                this.textBoxConPName.Text = "";
                this.textBoxConPPhone.Text = "";
                this.textBoxAddress.Text = "";
                this.textBoxBank.Text = "";
                this.textBoxAccount.Text = "";
                this.textBoxFax.Text = "";
                this.textBoxManagerName.Text = "";
                this.textBoxMngTel.Text = "";
                this.richTextBoxNote.Text = "";
            }
        }

        private void dataGridViewCompList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCompList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.DZCompDataTab modelDZCompDataTab = new DGBY.Model.DZCompDataTab();
                modelDZCompDataTab.DZCompName = this.textBoxCompName.Text;
                modelDZCompDataTab.DZCompNameJX = this.textBoxCompAbbr.Text;
                modelDZCompDataTab.DZCompTel = this.textBoxCompPhone.Text;
                modelDZCompDataTab.DZConPeoName = this.textBoxConPName.Text;
                modelDZCompDataTab.DZConPeoTel = this.textBoxConPPhone.Text;
                modelDZCompDataTab.DZCompAddr = this.textBoxAddress.Text;
                modelDZCompDataTab.DZKHBank = this.textBoxBank.Text;
                modelDZCompDataTab.DZCountNo = this.textBoxAccount.Text;
                modelDZCompDataTab.DZCompFax = this.textBoxFax.Text;
                modelDZCompDataTab.ManagerName = this.textBoxManagerName.Text;
                modelDZCompDataTab.ManagerTel = this.textBoxMngTel.Text;
                modelDZCompDataTab.Note = this.richTextBoxNote.Text;

                int nResult = m_bllDZCompDataTab.Add(modelDZCompDataTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", modelDZCompDataTab.DZCompName);

                    m_nSelectDZCompDataTabID = m_bllDZCompDataTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllDZCompDataTab.GetModel(m_nSelectDZCompDataTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.DZCompDataTab modelDZCompDataTab = m_bllDZCompDataTab.GetModel(m_nSelectDZCompDataTabID);
                //modelDZCompDataTab.DZCompDataTabID = m_nSelectDZCompDataTabID;
                modelDZCompDataTab.DZCompName = this.textBoxCompName.Text;
                modelDZCompDataTab.DZCompNameJX = this.textBoxCompAbbr.Text;
                modelDZCompDataTab.DZCompTel = this.textBoxCompPhone.Text;
                modelDZCompDataTab.DZConPeoName = this.textBoxConPName.Text;
                modelDZCompDataTab.DZConPeoTel = this.textBoxConPPhone.Text;
                modelDZCompDataTab.DZCompAddr = this.textBoxAddress.Text;
                modelDZCompDataTab.DZKHBank = this.textBoxBank.Text;
                modelDZCompDataTab.DZCountNo = this.textBoxAccount.Text;
                modelDZCompDataTab.DZCompFax = this.textBoxFax.Text;
                modelDZCompDataTab.ManagerName = this.textBoxManagerName.Text;
                modelDZCompDataTab.ManagerTel = this.textBoxMngTel.Text;
                modelDZCompDataTab.Note = this.richTextBoxNote.Text;


                m_bllDZCompDataTab.Update(modelDZCompDataTab);

                Frm_Main.Log2DB(this.Text, "修改", modelDZCompDataTab.DZCompName);

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (m_bllDZCompDataTab.GetModel(m_nSelectDZCompDataTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllDZCompDataTab.Delete(m_nSelectDZCompDataTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickDZComp();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel文件(*.xls)|*.xls";
            string outFile = "";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                outFile = sfd.FileName;
            }
            else
            {
                return;
            }

            string templetFile = templetFilePath + "DZCompInfo.xlt";

            DataSet ds = m_bllDZCompDataTab.GetAllList();
            DataTable dt = ds.Tables[0];
            ExcelBase eB = new ExcelBase(templetFile, outFile);

            try
            {
                eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
            }
            catch (System.Exception ex)
            {

            }

            eB.Close();
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            string strTemp = this.comboBoxQueryCondition.SelectedItem.ToString();

            strWhere = dicQueryCondition[strTemp] + " LIKE " + "'%" + this.textBoxValueIs.Text + "%'";


            //string strOrderBy = "\nORDER BY FactoryCode";
            //strWhere = strWhere + strOrderBy;
            DataSet ds = new DataSet();
            ds = m_bllDZCompDataTab.GetList(strWhere);

            dataGridViewCompList.DataSource = null;
            dataGridViewCompList.DataSource = ds.Tables[0];
            if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
            {
                this.dataGridViewCompList.DataSource = ds.Tables[0];
                this.dataGridViewCompList.Columns[0].Visible = false;
            }
            else
            {
                this.dataGridViewCompList.DataSource = null;
            }

            return;
        }

        private void buttonRecvPerson_Click(object sender, EventArgs e)
        {
            Frm_DZCompRecvP frm_DZCompRecvP = new Frm_DZCompRecvP();
            frm_DZCompRecvP.StartPosition = FormStartPosition.CenterScreen;
            frm_DZCompRecvP.ShowDialog();
        }




    }
}